import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import guess_lexer, JavaLexer

from digvcs.lib.base import BaseController, render
from digvcs.model.meta import Session
from digvcs.model.svnrepo import *

log = logging.getLogger(__name__)

class BrowseController(BaseController):

    def index(self): 
        return redirect_to('repositories')

    def repositories(self):
        c.repositories = Session.query(Repository).order_by('url').all() 
        return render('browse/repositories.html')
        
    def revisions(self, id): 
        c.revisions = Session.query(Revision).filter_by(repository_id=id).order_by('rev_num').all()
        return render('browse/revisions.html')
        
    def changes(self, revision=27): 
        c.changes = Session.query(Change).filter_by(revision_id=revision).all()
        return render('browse/changes.html')
        
    def show(self):
        c.style = HtmlFormatter().get_style_defs('.highlight')
        c.fragment = """
package org.digvcs;

import java.util.*;

public class Main() {
    public static void main(String...args) {
        System.out.println("Dupa");
    }
}
        """
        c.fragment = highlight(c.fragment, JavaLexer(), HtmlFormatter())
        return render('browse/show.html')
        